package com.bg.stock.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bg.stock.entity.Stock;
import com.bg.stock.service.IStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author bg
 * @since 2023-02-09
 */
@RestController
@RequestMapping("/stock")
public class StockController {

    @Autowired
    private IStockService stockService;


    @RequestMapping("/deduct")
    public void deduct(String commodityCode, Integer count) {
        QueryWrapper<Stock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("commodity_code", commodityCode);
        Stock stock = stockService.getOne(queryWrapper);
        if (null == stock)
            throw new RuntimeException("异常:模拟业务异常:库存不存在");

        if (stock.getCount() < count)
            throw new RuntimeException("异常:模拟业务异常:库存不足");

        stock.setCount(stock.getCount() - count);
        stockService.updateById(stock);

    }
}
